package cluster;

import java.util.HashMap;
import java.io.File;
import java.io.FileInputStream;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Set;
import java.util.Iterator;


public class Arquivo {

    private Integer grupo;

    private HashMap<String,Integer> freq;

    private Boolean centroide;
    
    private String nome;
    
    private Integer qtdPalavras;

	public Arquivo(String path) {

         this.grupo = 0;

         this.centroide = false;

         this.freq = new HashMap<String,Integer>();
         this.freq.clear();

         File stopwords=new File("C:\\english");
         File arq1= new File(path);

         FileInputStream stop=null;
         FileInputStream f1=null;

         try{
             stop = new FileInputStream(stopwords);
             f1 = new FileInputStream(arq1);
         }
         catch(Exception e){
             System.out.println(e);
         }


         Scanner scannerstop = new Scanner(stop);
         Scanner scannerf1 = new Scanner(f1);

         String aux;
         int flagstop=0;
         int flagarq=0;

         ArrayList <String> swords;
         swords = new ArrayList <String>();
         swords.clear();

         while(scannerstop.hasNext()){
                 swords.add(converte(scannerstop.next()));
         }

         while(scannerf1.hasNext()){
             flagstop = 0;
             flagarq = 0;

             aux = converte(scannerf1.next());

             for(int i=0;i<swords.size();i++){
                 if(aux.equals(swords.get(i).toString()) || (aux.isEmpty()) || (aux.length()<=3)){
                     flagstop=1;
                     break;
                 }
             }

            

             if(flagstop==0)
             {
                 if(this.freq.containsKey(aux))
                 {
                    this.freq.put(aux,this.freq.get(aux)+1);
                 }
                 else
                 {
                   this.freq.put(aux,1);
                 }
             }
             
         }


    }

    public Integer getGrupo() {
		return grupo;
	}

	public void setGrupo(Integer grupo) {
		this.grupo = grupo;
	}

	public HashMap<String, Integer> getFreq() {
		return freq;
	}

	public void setFreq(HashMap<String, Integer> freq) {
		this.freq = freq;
	}

	public Boolean getCentroide() {
		return centroide;
	}

	public void setCentroide(Boolean centroide) {
		this.centroide = centroide;
	}
	
	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public Integer getQtdPalavras() {
		return qtdPalavras;
	}

	public void setQtdPalavras(Integer qtdPalavras) {
		this.qtdPalavras = qtdPalavras;
	}

	public static String converte(String texto){

        texto = texto.replace(">","");
        texto = texto.replace(">>","");
        texto = texto.replace(">>>","");
        texto = texto.replace("?","");
        texto = texto.replace(":","");
        texto = texto.replace("(","");
        texto = texto.replace(")","");
        texto = texto.replace(",","");
        texto = texto.replace("\"","");
        texto = texto.replace("!","");
        texto = texto.replace("<","");
        texto = texto.replace("-","");
        texto = texto.replace("--","");
        texto = texto.replace("*","");
        texto = texto.replace("_","");
        if(texto.endsWith("."))
            texto = texto.replace(".","");

        return texto.toLowerCase();
    }

	public float Distancia(Arquivo novo){
        float aux = 0 ;
        String key = null;
        Set s = novo.freq.keySet();
        Iterator t = s.iterator();
        while(t.hasNext())
        {
            key = (String) t.next();
            if(this.freq.containsKey(key)){
                aux += (this.freq.get(key)-novo.freq.get(key))*(this.freq.get(key)-novo.freq.get(key));
            }
            else
            {
                aux+=(novo.freq.get(key)*novo.freq.get(key));
            }
        }
        return aux;
    }

     public float Distancia(HashMap<String,Integer> auxiliar){
        float aux = 0 ;
        String key = null;
        Set s = auxiliar.keySet();
        Iterator t = s.iterator();
        while(t.hasNext())
        {
            key = (String) t.next();
            if(this.freq.containsKey(key)){
                aux += (this.freq.get(key)-auxiliar.get(key))*(this.freq.get(key)-auxiliar.get(key));
            }
            else
            {
                aux+=(auxiliar.get(key)*auxiliar.get(key));
            }
        }
        return aux;
    }

}
